//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "ProjectedCostFunction.h"
using namespace Cephei::QL::Math::Optimization;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Math/Optimization/CostFunction.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Optimization::CProjectedCostFunction::CProjectedCostFunction (Cephei::QL::Math::Optimization::ICostFunction^ costFunction, Cephei::QL::Math::IArray^ parametersValues, Cephei::IVector<Boolean>^ parametersFreedoms) : CCostFunction(CProjectedCostFunction::typeid)
{
    CCostFunction^ _CcostFunction;
    CArray^ _CparametersValues;
    CoVector<Boolean>^ _CparametersFreedoms;
    try
    {
#ifdef HANDLE
        _phProjectedCostFunction = NULL;
#endif
        _CcostFunction = safe_cast<CCostFunction^> (costFunction);
        _CcostFunction->Lock();
        QuantLib::CostFunction& _costFunction = static_cast<QuantLib::CostFunction&> (_CcostFunction->GetReference ()); 
        _CparametersValues = safe_cast<CArray^> (parametersValues);
        _CparametersValues->Lock();
        QuantLib::Array& _parametersValues = static_cast<QuantLib::Array&> (_CparametersValues->GetReference ()); 
        CoVector<Boolean>^ _CparametersFreedoms = safe_cast<CoVector<Boolean>^> (parametersFreedoms);
        _CparametersFreedoms->Lock();
        INativeVector<Boolean>^ _NCIparametersFreedoms = _CparametersFreedoms->getFeature (NativeFeature::Value);
        CBooleanVector^ _NCparametersFreedoms = safe_cast<CBooleanVector^>(_NCIparametersFreedoms);
        std::vector<bool>& _parametersFreedoms = static_cast<std::vector<bool>&> (_NCparametersFreedoms->GetReference ());
        _ppProjectedCostFunction = new boost::shared_ptr<QuantLib::ProjectedCostFunction> (new QuantLib::ProjectedCostFunction ( _costFunction,  _parametersValues,  _parametersFreedoms ));
        SetCostFunction (boost::dynamic_pointer_cast<QuantLib::CostFunction> (*_ppProjectedCostFunction));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CcostFunction != nullptr) _CcostFunction->Unlock();
        if (_CparametersValues != nullptr) _CparametersValues->Unlock();
        if (_CparametersFreedoms != nullptr) _CparametersFreedoms->Unlock();
    }
}
Cephei::QL::Math::Optimization::CProjectedCostFunction::CProjectedCostFunction (boost::shared_ptr<QuantLib::ProjectedCostFunction>& childNative, Object^ owner) : CCostFunction(CProjectedCostFunction::typeid)
{
#ifdef HANDLE
	_phProjectedCostFunction = NULL;
#endif
	_ppProjectedCostFunction = &childNative;
    _ppCostFunction = new boost::shared_ptr<QuantLib::CostFunction> (boost::dynamic_pointer_cast<QuantLib::CostFunction> (*_ppProjectedCostFunction));
}
Cephei::QL::Math::Optimization::CProjectedCostFunction::CProjectedCostFunction (QuantLib::ProjectedCostFunction& childNative, Object^ owner) : CCostFunction(CProjectedCostFunction::typeid)
{
#ifdef HANDLE
	_phProjectedCostFunction = NULL;
#endif
	_ppProjectedCostFunction = new boost::shared_ptr<QuantLib::ProjectedCostFunction> (&childNative);
    _ppCostFunction = new boost::shared_ptr<QuantLib::CostFunction> (boost::dynamic_pointer_cast<QuantLib::CostFunction> (*_ppProjectedCostFunction));
    _ProjectedCostFunctionOwner = owner;
    _CostFunctionOwner = owner;
}

Cephei::QL::Math::Optimization::CProjectedCostFunction::CProjectedCostFunction (CProjectedCostFunction^ copy) : CCostFunction(CProjectedCostFunction::typeid)
{
#ifdef HANDLE
	_phProjectedCostFunction = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppProjectedCostFunction = new boost::shared_ptr<QuantLib::ProjectedCostFunction> (copy->GetShared());
        _ppCostFunction = new boost::shared_ptr<QuantLib::CostFunction> (boost::dynamic_pointer_cast<QuantLib::CostFunction> (*_ppProjectedCostFunction));
    }
}
Cephei::QL::Math::Optimization::CProjectedCostFunction::CProjectedCostFunction (System::Type^ t) : CCostFunction(CProjectedCostFunction::typeid)
{
#ifdef HANDLE
	_phProjectedCostFunction = NULL;
#endif
	if (!t->IsSubclassOf(CProjectedCostFunction::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Optimization::CProjectedCostFunction::CProjectedCostFunction (QuantLib::Handle<QuantLib::ProjectedCostFunction>& childNative, Object^ owner)  : CCostFunction(CProjectedCostFunction::typeid)
{
	_phProjectedCostFunction = &childNative;
	_ppProjectedCostFunction = &static_cast<boost::shared_ptr<QuantLib::ProjectedCostFunction>>(childNative.currentLink());
    _ppCostFunction = new boost::shared_ptr<QuantLib::CostFunction> (boost::dynamic_pointer_cast<QuantLib::CostFunction> (*_ppProjectedCostFunction));
    _ProjectedCostFunctionOwner = owner;
}
Cephei::QL::Math::Optimization::CProjectedCostFunction::CProjectedCostFunction (QuantLib::Handle<QuantLib::ProjectedCostFunction> childNative)  : CCostFunction(CProjectedCostFunction::typeid)
{
	_phProjectedCostFunction = &childNative;
	_ppProjectedCostFunction = &static_cast<boost::shared_ptr<QuantLib::ProjectedCostFunction>>(childNative.currentLink());
    _ppCostFunction = new boost::shared_ptr<QuantLib::CostFunction> (boost::dynamic_pointer_cast<QuantLib::CostFunction> (*_ppProjectedCostFunction));
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Optimization::CProjectedCostFunction::CProjectedCostFunction (QuantLib::ProjectedCostFunction childNative)  : CCostFunction(CProjectedCostFunction::typeid)
{
#ifdef HANDLE
	_phProjectedCostFunction = NULL;
#endif
	_ppProjectedCostFunction = new boost::shared_ptr<QuantLib::ProjectedCostFunction> (new QuantLib::ProjectedCostFunction (childNative));
    _ppCostFunction = new boost::shared_ptr<QuantLib::CostFunction> (boost::dynamic_pointer_cast<QuantLib::CostFunction> (*_ppProjectedCostFunction));
}
#endif

Cephei::QL::Math::Optimization::CProjectedCostFunction::~CProjectedCostFunction ()
{
    if (_ppProjectedCostFunction != NULL)
    {
	    delete _ppProjectedCostFunction;
        _ppProjectedCostFunction = NULL;
    }
}
Cephei::QL::Math::Optimization::CProjectedCostFunction::!CProjectedCostFunction ()
{
    if (_ppProjectedCostFunction != NULL)
    {
	    delete _ppProjectedCostFunction;
    }
}
QuantLib::ProjectedCostFunction& Cephei::QL::Math::Optimization::CProjectedCostFunction::GetReference ()
{
    if (_ppProjectedCostFunction == NULL) throw gcnew NativeNullException ();
	return **_ppProjectedCostFunction;
}
boost::shared_ptr<QuantLib::ProjectedCostFunction>& Cephei::QL::Math::Optimization::CProjectedCostFunction::GetShared ()
{
    if (_ppProjectedCostFunction == NULL) throw gcnew NativeNullException ();
	return *_ppProjectedCostFunction;
}
QuantLib::ProjectedCostFunction* Cephei::QL::Math::Optimization::CProjectedCostFunction::GetPointer ()
{
    if (_ppProjectedCostFunction == NULL) throw gcnew NativeNullException ();
	return &**_ppProjectedCostFunction;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ProjectedCostFunction>& Cephei::QL::Math::Optimization::CProjectedCostFunction::GetHandle ()
{
	if (_phProjectedCostFunction == NULL)
	{
		_phProjectedCostFunction = new Handle<QuantLib::ProjectedCostFunction> (*_ppProjectedCostFunction);
	}
	return *_phProjectedCostFunction;
}
#endif
bool Cephei::QL::Math::Optimization::CProjectedCostFunction::HasNative () 
{
	return (_ppProjectedCostFunction != NULL);
}

Double Cephei::QL::Math::Optimization::CProjectedCostFunction::Value (Cephei::QL::Math::IArray^ freeParameters)
{
    CArray^ _CfreeParameters;
    try
    {
        _CfreeParameters = safe_cast<CArray^> (freeParameters);
        _CfreeParameters->Lock();
        QuantLib::Array& _freeParameters = static_cast<QuantLib::Array&> (_CfreeParameters->GetReference ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppProjectedCostFunction)->value ( _freeParameters );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CfreeParameters != nullptr) _CfreeParameters->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Optimization::IProjectedCostFunction^ Cephei::QL::Math::Optimization::CProjectedCostFunction_Factory::Create (Cephei::QL::Math::Optimization::ICostFunction^ costFunction, Cephei::QL::Math::IArray^ parametersValues, Cephei::IVector<Boolean>^ parametersFreedoms)
{
    return gcnew CProjectedCostFunction ( costFunction,  parametersValues,  parametersFreedoms);
}
